<%- include('css') %>
<div id="app" style="width:980px">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>编辑干洗中心</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">中心名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" placeholder="请输入中心名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">负责人姓名</label>
            <div class="layui-input-block">
                <input type="text" name="realname" lay-verify="required|realname" placeholder="请输入负责人姓名" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" maxlength="11" name="phone" lay-verify="required|phone" placeholder="请输入联系电话" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">省市区</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="on" class="layui-input" lay-verify="required" id="city-picker" name="province" readonly="readonly" data-toggle="city-picker" placeholder="请选择">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">具体地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" lay-verify="required" placeholder="请输入具体地址" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="update">保存</button>
                <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </form>
</div>


<%- include('script') %>
<script src="/public/js_module/city-picker/city-picker.data.js"></script>
<link href="/public/js_module/city-picker/city-picker.css" rel="stylesheet" />
<script src="/public/js_module/city-picker/city-picker.js" type="text/javascript" charset="utf-8"></script>
<script>
    var cityPicker = layui.citypicker;

    var vm = new Vue({
        el: "#app",
        data: function () {
            return {
                isButtonDisabled: false,
                id:'<%=id%>',

            }

        },
        mounted: function () {
            let self = this

            layui.use(['form'], function () {
                var form = layui.form;

                var currentPicker = new cityPicker("#city-picker", {
                    provincename:"provinceId",
                    cityname:"cityId",
                    districtname: "districtId",
                    level: 'districtId',// 级别
                });

                //定义验证规则
                // form.verify({
                    // path: function(value, item){ //value：表单的值、item：表单的DOM对象
                    //     if(!new RegExp("^/[a-z]+/([a-z]+|[a-z]+/[a-z]+|[a-z]+/[a-z]+/[a-z]+)$").test(value)){
                    //         return '路径必须以\'/\'开头,且只能包含\'/和小写字母\'';
                    //     }
                    // },
                    //我们既支持上述函数式的方式，也支持下述数组的形式
                    //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
                    // code: [
                    //     /^[A-Za-z]+$/
                    //     ,'编号只能包含字母,不能有中文或者下划线等字符'
                    // ],
                    // name:[/^.{1,20}$/,'只能填写长度为1-20的所有字符']
                // });

                var load = layer.load();
                $.ajax({
                    url:'/admin/center/'+self.id,
                    type:'GET',
                    data:{},
                    success:(res)=>{
                        if(res.code === 200){
                            currentPicker.setValue(res.data.province);
                            form.val("form",res.data);
                        }else{
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            parent.layer.msg('获取数据失败：'+res.error+' -- '+res.code,{icon:2});
                        }
                    },
                    complete:()=>{
                        layer.close(load);
                    }
                });

                //监听提交
                form.on('submit(update)', function (data) {
                    self.isButtonDisabled = true;
                    $.ajax({
                        url:'/admin/center/'+self.id,
                        type:'PUT',
                        data:{data:data.field},
                        success:(res)=>{
                            if(res.code === 200){
                                parent.vm.iframeupdate()
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.layer.msg('修改成功!',{icon:1})
                            }else{
                                layer.msg('修改失败：'+res.error+' -- '+res.code,{icon:2});
                            }
                            self.isButtonDisabled = false;
                        }
                    });
                    return false;
                })
            });
        },
        methods: {
            doclose: function () {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            },
        }
    })
</script>
